#include "common.h"
#include <Preferences.h>

static Preferences myPrefs;

#define RW_MODE false
#define RO_MODE true

void saveConfigToNVS() {
  myPrefs.begin(PREF_CFG_NAMESPACE, RW_MODE);

  myPrefs.putInt(PREF_KEYS_PORT_1_CURRENT1, inaData[0].currAlert);
  myPrefs.putInt(PREF_KEYS_PORT_1_CURRENT2, inaData[0].currCutOff);
  myPrefs.putInt(PREF_KEYS_PORT_1_CURRENT3, inaData[0].chartMin);
  myPrefs.putInt(PREF_KEYS_PORT_1_CURRENT4, inaData[0].chartMax);

  myPrefs.putInt(PREF_KEYS_PORT_2_CURRENT1, inaData[1].currAlert);
  myPrefs.putInt(PREF_KEYS_PORT_2_CURRENT2, inaData[1].currCutOff);
  myPrefs.putInt(PREF_KEYS_PORT_2_CURRENT3, inaData[1].chartMin);
  myPrefs.putInt(PREF_KEYS_PORT_2_CURRENT4, inaData[1].chartMax);

  myPrefs.putInt(PREF_KEYS_PORT_3_CURRENT1, inaData[2].currAlert);
  myPrefs.putInt(PREF_KEYS_PORT_3_CURRENT2, inaData[2].currCutOff);
  myPrefs.putInt(PREF_KEYS_PORT_3_CURRENT3, inaData[2].chartMin);
  myPrefs.putInt(PREF_KEYS_PORT_3_CURRENT4, inaData[2].chartMax);

  myPrefs.putInt(PREF_KEYS_PORT_4_CURRENT1, inaData[3].currAlert);
  myPrefs.putInt(PREF_KEYS_PORT_4_CURRENT2, inaData[3].currCutOff);
  myPrefs.putInt(PREF_KEYS_PORT_4_CURRENT3, inaData[3].chartMin);
  myPrefs.putInt(PREF_KEYS_PORT_4_CURRENT4, inaData[3].chartMax);

  myPrefs.end();
}

void loadConfigFromNVS() {
  myPrefs.begin(PREF_CFG_NAMESPACE, RW_MODE);

  inaData[0].currAlert = myPrefs.getInt(PREF_KEYS_PORT_1_CURRENT1, DEFAULT_ALERT_CURRENT);
  inaData[0].currCutOff = myPrefs.getInt(PREF_KEYS_PORT_1_CURRENT2, DEFAULT_CUT_OFF_CURRENT);
  inaData[0].chartMin = myPrefs.getInt(PREF_KEYS_PORT_1_CURRENT3, DEFAULT_CHART_MIN_CURRENT);
  inaData[0].chartMax = myPrefs.getInt(PREF_KEYS_PORT_1_CURRENT4, DEFAULT_CHART_MAX_CURRENT);

  inaData[1].currAlert = myPrefs.getInt(PREF_KEYS_PORT_2_CURRENT1, DEFAULT_ALERT_CURRENT);
  inaData[1].currCutOff = myPrefs.getInt(PREF_KEYS_PORT_2_CURRENT2, DEFAULT_CUT_OFF_CURRENT);
  inaData[1].chartMin = myPrefs.getInt(PREF_KEYS_PORT_2_CURRENT3, DEFAULT_CHART_MIN_CURRENT);
  inaData[1].chartMax = myPrefs.getInt(PREF_KEYS_PORT_2_CURRENT4, DEFAULT_CHART_MAX_CURRENT);

  inaData[2].currAlert = myPrefs.getInt(PREF_KEYS_PORT_3_CURRENT1, DEFAULT_ALERT_CURRENT);
  inaData[2].currCutOff = myPrefs.getInt(PREF_KEYS_PORT_3_CURRENT2, DEFAULT_CUT_OFF_CURRENT);
  inaData[2].chartMin = myPrefs.getInt(PREF_KEYS_PORT_3_CURRENT3, DEFAULT_CHART_MIN_CURRENT);
  inaData[2].chartMax = myPrefs.getInt(PREF_KEYS_PORT_3_CURRENT4, DEFAULT_CHART_MAX_CURRENT);

  inaData[3].currAlert = myPrefs.getInt(PREF_KEYS_PORT_4_CURRENT1, DEFAULT_ALERT_CURRENT);
  inaData[3].currCutOff = myPrefs.getInt(PREF_KEYS_PORT_4_CURRENT2, DEFAULT_CUT_OFF_CURRENT);
  inaData[3].chartMin = myPrefs.getInt(PREF_KEYS_PORT_4_CURRENT3, DEFAULT_CHART_MIN_CURRENT);
  inaData[3].chartMax = myPrefs.getInt(PREF_KEYS_PORT_4_CURRENT4, DEFAULT_CHART_MAX_CURRENT);

  myPrefs.end();
}
